<template>
  <div v-loading="loading">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px" class="dataForm">
      <el-row>
        <el-col :span="12">
          <el-form-item :label="$t('年份')" prop="year">
            <DatePicker
              v-model="form.year"
              value-format="yyyy"
              format="yyyy"
              type="year"
              :placeholder="$t('年份')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('季度')" prop="quarter">
            <dictSelect
              v-model="form.quarter"
              :clearable="true"
              dictionary="deal_report_period"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('数据起始时间')" prop="startDate">
            <DatePicker v-model="form.startDate" :placeholder="$t('数据起始时间')" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('数据结束时间')" prop="endDate">
            <DatePicker v-model="form.endDate" :placeholder="$t('数据结束时间')" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('报告类型')" prop="type">
            <dictSelect
              v-model="form.type"
              :clearable="true"
              dictionary="deal_report_type"
              @change="typeOnChange"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item v-if="isMultiple === false" :label="$t('项目名称')" prop="dealId">
            <DealSelect
              v-model="form.dealId"
              deal-status="TH"
              :placeholder="$t('项目名称')"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <MultipleDeal v-if="isMultiple === true" v-model="form.dealIdArray" height="400" />
    </el-form>
    <div slot="footer" class="dialog-footer" style="text-align: right;">
      <!--保存，保存并启动流程，取消 三个按钮-->
      <el-button @click="cancel">{{ $t("取消") }}</el-button>
      <el-button v-hasPermi="['deal:report:edit']" type="primary" @click="submitForm">{{ $t("保存") }}</el-button>
    </div>
  </div>
</template>

<script>
    import { saveDealReport, getDealReport, isMultiple } from '@/api/deal/report/createReport/dealReport'
    import DealSelect from '@/components/Select/dealSelect'
    import MultipleDeal from '@/components/Deal/multipleDeal'
    export default {
        components: {
          DealSelect,
          MultipleDeal
        },
        data() {
            return {
                // 表单参数
                form: {},
                // 遮罩层
                loading: false,
                // 是否支持多选项目
                isMultiple: false,
                // 表单校验
                rules: {
                  year: [
                    { required: true, message: this.$t('必填信息'), trigger: 'blur' }
                  ], quarter: [
                    { required: true, message: this.$t('必填信息'), trigger: ['blur', 'change'] }
                  ], startDate: [
                    { required: true, message: this.$t('必填信息'), trigger: ['blur', 'change'] }
                  ], endDate: [
                    { required: true, message: this.$t('必填信息'), trigger: ['blur', 'change'] }
                  ], type: [
                    { required: true, message: this.$t('必填信息'), trigger: ['blur', 'change'] }
                  ], dealId: [
                    { required: true, message: this.$t('必填信息'), trigger: ['blur', 'change'] }
                  ]
                }
            }
        },
        watch: {
          // 季度
          'form.quarter': function() {
            // 设置数据起始时间和结束时间
            this.setTime()
          },
          // 年份
          'form.year': function() {
            // 设置数据起始时间和结束时间
            this.setTime()
          }
        },
        created() {
        },
        methods: {
            // 初始化数据
            init(data) {
              if (data != null && data.id !== undefined) {
                this.loading = true
                getDealReport(data.id).then(response => {
                  this.form = response.data
                  this.loading = false
                })
              } else {
                this.reset()
              }
            },
            // 表单重置
            reset() {
              this.form = {
                id: undefined,
                year: undefined,
                quarter: undefined,
                startDate: undefined,
                endDate: undefined,
                type: undefined,
                dealId: undefined,
                dealIdArray: [],
                dealReportDealList: [],
                createTime: undefined,
                createBy: undefined,
                updateTime: undefined,
                updateBy: undefined,
                delFlag: undefined
              }
                this.resetForm('form')
            },
            // 取消按钮
            cancel() {
              if (this.$parent.$parent.formOpen !== undefined) {
                this.$parent.$parent.formOpen = false
              }
              if (this.$parent.$parent.reset !== undefined) {
                this.$parent.$parent.reset()
              }
              if (this.$parent.$parent.$parent.$parent.formOpen !== undefined) {
                this.$parent.$parent.$parent.$parent.formOpen = false
              }
              if (this.$parent.$parent.$parent.$parent.reset !== undefined) {
                this.$parent.$parent.$parent.$parent.reset()
              }
            },
            // 关闭form页面
            closeForm() {
              this.loading = false
              if (this.$parent.$parent.formOpen !== undefined) {
                this.$parent.$parent.formOpen = false
              }
              if (this.$parent.$parent.getList !== undefined) {
                this.$parent.$parent.getList()
              }
              if (this.$parent.$parent.$parent.$parent.formOpen !== undefined) {
                this.$parent.$parent.$parent.$parent.formOpen = false
              }
              if (this.$parent.$parent.$parent.$parent.getList !== undefined) {
                this.$parent.$parent.$parent.$parent.getList()
              }
            },
            /** 提交按钮 */
            submitForm: function() {
              if (this.isMultiple) {
                if (this.form.dealIdArray === null || this.form.dealIdArray === [] || this.form.dealIdArray.length === 0) {
                  this.msgError(this.$t('请选择项目'))
                  return false
                }
              }
              this.$refs['form'].validate(valid => {
                if (valid) {
                  this.loading = true
                  saveDealReport(this.form).then(response => {
                    if (response.code === 200) {
                        this.msgSuccess(this.$t('保存成功'))
                        this.closeForm()
                    } else {
                        this.msgError(response.msg)
                    }
                    this.loading = false
                  })
                } else {
                  this.locationError(document)
                }
              })
           },
          // 报告类型修改
          typeOnChange() {
            isMultiple(this.form.type).then(response => {
              if (response.code === 200) {
                this.isMultiple = response.data
              } else {
                this.msgError(response.msg)
              }
              this.loading = false
            })
          },
          // 设置数据起始时间和结束时间
          setTime() {
            if (this.form.year !== undefined && this.form.quarter !== undefined) {
              if (this.form.quarter === 'FIRST') { // 一季度
                this.form.startDate = this.form.year + '-01-01'
                this.form.endDate = this.form.year + '-03-31'
              } else if (this.form.quarter === 'SECOND') { // 二季度
                this.form.startDate = this.form.year + '-04-01'
                this.form.endDate = this.form.year + '-06-30'
              } else if (this.form.quarter === 'THIRD') { // 三季度
                this.form.startDate = this.form.year + '-07-01'
                this.form.endDate = this.form.year + '-09-30'
              } else if (this.form.quarter === 'FOURTH') { // 四季度
                this.form.startDate = this.form.year + '-10-01'
                this.form.endDate = this.form.year + '-12-31'
              }
            }
          }
        }

    }
</script>
